package view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JButton;
import javax.swing.JPanel;

import model.Auction;
import model.AuctionCalendar;
import model.WestPanelEnum;
import controller.ACSystem;

import javax.swing.border.LineBorder;

import java.awt.Color;
import java.util.Observable;
/**
 * 
 * @author Bob Landowski
 * @author Kevin Smith
 *
 */
@SuppressWarnings("serial")
public class EastPanelNP extends JPanel {
	
	/** The Accontroller */
	private ACSystem my_ACcontroller;
	
	/** The EastPanel Observable */
	private EastPanelObservable epo = new EastPanelObservable();
	
	/** The main frame */
	private JFrame my_mainframe;
	
	/** The auction selected */
	private String auctionSelected;
	
	/** The auction searched */
	private Auction auctionSearched;
	
	/** The Calendar */
	private AuctionCalendar my_auction_calendar;
	
	/** The reqAuctionButton */
	private JButton reqAuctionButton;

	/**
	 * Create the panel.
	 */
	public EastPanelNP(final ACSystem the_ACcontroller) {
		setBorder(new LineBorder(new Color(0, 0, 0), 5));
		setLayout(null);
		setPreferredSize(new Dimension(180, 400));
		my_ACcontroller = the_ACcontroller;
		my_auction_calendar = my_ACcontroller.getAuctionCalendar();
		epo.addObserver(my_ACcontroller);
		JButton calendarButton = new JButton("Calendar");
		calendarButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_ACcontroller.setWestPanel(WestPanelEnum.CALENDAR); 
				epo.buttonClicked(e.getSource());			}
		});
		calendarButton.setBounds(15, 26, 148, 29);
		add(calendarButton);

		JButton exitButton = new JButton("Exit");
		exitButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				System.exit(0);

			}
		});
		exitButton.setBounds(15, 296, 148, 29);
		add(exitButton);

		reqAuctionButton = new JButton("Request Auction");
		reqAuctionButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_ACcontroller.setWestPanel(WestPanelEnum.AUCTION_INPUT);
				epo.buttonClicked(e.getSource());
			}
		});
		reqAuctionButton.setBounds(15, 166, 148, 29);
		add(reqAuctionButton);

		JButton listAuctionsButton = new JButton("List Auctions");
		listAuctionsButton.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {

				Object[] possibilities = new Object[my_ACcontroller
						.getAuctionList().size()];
				for (int i = 0; i < my_ACcontroller.getAuctionList().size(); i++) {
					possibilities[i] = my_ACcontroller.getAuctionList().get(i)
							.getName();
				}

				auctionSelected = (String) JOptionPane.showInputDialog(
						my_mainframe, "Choose your Auction", "Auction chooser",
						JOptionPane.PLAIN_MESSAGE, null, possibilities, null);
				if (auctionSelected == null) {
					
				} else {
					auctionSearched = my_auction_calendar.findAuction(auctionSelected);	
					my_ACcontroller.setWestPanel(WestPanelEnum.AUCTION);
					my_ACcontroller.getMainFrame().westPanelState(auctionSearched);
				}
			}

		});
		listAuctionsButton.setBounds(15, 96, 148, 29);
		add(listAuctionsButton);
		
		JButton addItemButton = new JButton("Add Item");
		addItemButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				my_ACcontroller.setWestPanel(WestPanelEnum.ITEMINPUT);
				my_ACcontroller.getMainFrame().westPanelState(new Object());
				
			}
		});
		addItemButton.setBounds(15, 236, 148, 29);
		add(addItemButton);
	}
	
	/** The disableMe */
	public void disableMe() {
		reqAuctionButton.setEnabled(false);
	}

	/** The east panel observable */
	private class EastPanelObservable extends Observable {
		/** Wussy constructor */
		EastPanelObservable() {
			super();
		}

		void buttonClicked(Object o) {
			setChanged();
			notifyObservers(o);
		}
	}
}
